package com.frcdi.qms.framework.interceptor;

/**
 * @ClassName CharacterTypeInterceptor
 * @Author Mr.Han
 * @Description //TODO $
 * @create 2024/7/28 14:13
 **/
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.PreparedStatement;
import java.util.Properties;

@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {PreparedStatement.class, Integer.class})
})
public class CharacterTypeInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");

        // 获取参数对象
        Object parameterObject = statementHandler.getBoundSql().getParameterObject();

        // 将所有字符类型转化为字符串类型
        if (parameterObject != null) {
            convertCharTypeToString(parameterObject);
        }

        // 继续执行后续拦截器和目标方法
        return invocation.proceed();
    }

    private void convertCharTypeToString(Object parameterObject) throws IllegalAccessException {
        if (parameterObject instanceof Character) {
            parameterObject = parameterObject.toString();
        } else if (parameterObject instanceof Character[]) {
            Character[] chars = (Character[]) parameterObject;
            String[] strings = new String[chars.length];
            for (int i = 0; i < chars.length; i++) {
                strings[i] = chars[i].toString();
            }
            parameterObject = strings;
        } else if (parameterObject.getClass().isArray()) {
            // 如果是其他类型的数组，递归处理
            Object[] objects = (Object[]) parameterObject;
            for (int i = 0; i < objects.length; i++) {
                convertCharTypeToString(objects[i]);
            }
        } else if (parameterObject instanceof Iterable) {
            // 如果是集合类型，递归处理
            for (Object item : (Iterable<?>) parameterObject) {
                convertCharTypeToString(item);
            }
        } else {
            // 如果是其他类型的对象，使用反射遍历所有字段并处理字符类型
            for (java.lang.reflect.Field field : parameterObject.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                Object fieldValue = field.get(parameterObject);
                if (fieldValue instanceof Character) {
                    field.set(parameterObject, fieldValue.toString());
                } else if (fieldValue != null && !field.getType().isPrimitive()) {
                    convertCharTypeToString(fieldValue);
                }
            }
        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // 可以在这里设置一些自定义的属性
    }
}
